#include "Common.h"
#include "LoggerPlainText.h"

using namespace RayFrame;

LoggerPlainText::LoggerPlainText()
{
  m_file = NULL;
}

void LoggerPlainText::Create(STRING filename)
{
  m_filename = filename;

  internalCreate();
}

void LoggerPlainText::Destroy()
{
  internalDestroy();
}

void LoggerPlainText::Write(STRING content, LogLevel level)
{
  assert(m_file);

  fprintf(m_file, "%s\n", content.c_str());
}

void LoggerPlainText::WriteBanner(STRING content)
{
  assert(m_file);

  const int screen_width = 70;
  int length = content.length();
  int start = ( 80 - length ) / 2;
  int end = start + length;
  for(int i=0; i<start; i++)
    fprintf(m_file, "=");

  fprintf(m_file, "%s", content.c_str());

  for(int i=end-1; i<screen_width; i++)
    fprintf(m_file, "=");

  fprintf(m_file, "\n");
}

void LoggerPlainText::internalCreate()
{
  m_file = fopen(m_filename.c_str(), "w");
  if(m_file)
    m_isOK = true;
}

void LoggerPlainText::internalDestroy()
{
  fclose(m_file);
}
